package com.sry.blog.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sry.blog.sys.mapper.ArticleToTagMapper;
import com.sry.blog.sys.model.ArticleToTag;
import com.sry.blog.sys.service.ArticleToTagService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * InnoDB free: 10240 kB; (`article_id`) REFER `blog/article`(`id`) ON UPDATE CASCA 服务实现类
 * </p>
 *
 * @author SR_Y
 * @since 2022-01-05
 */
@Service
@Slf4j
public class ArticleToTagServiceImpl extends ServiceImpl<ArticleToTagMapper, ArticleToTag> implements ArticleToTagService {

    /**
     * 创建和修改文章类型关系
     *
     * @param articleToTag 文章类型关系
     * @return boolean
     */
    @Override
    public boolean creatOrUpdate(ArticleToTag articleToTag) {
        return saveOrUpdate(articleToTag);
    }

    /**
     * 获得文章类型关系(通过文章ID和类型ID)
     *
     * @param articleToTag 文章类型关系
     * @return 文章类习惯关系(获得ID)
     */
    @Override
    public ArticleToTag getOne(ArticleToTag articleToTag) {
        final Integer tagId = articleToTag.getTagId();
        final Integer articleId = articleToTag.getArticleId();
        QueryWrapper<ArticleToTag> wrapper = new QueryWrapper<>();
        LambdaQueryWrapper<ArticleToTag> lambda = wrapper.lambda();
        if (tagId!=null&&articleId!=null){
            lambda.eq(ArticleToTag::getArticleId,articleId).eq(ArticleToTag::getTagId,tagId);
        }
        return getOne(wrapper);
    }

    /**
     * 获得文章类型关系表
     *
     * @param articleToTag 文章类型
     * @return 文章类型列表
     */
    @Override
    public List<ArticleToTag> list(ArticleToTag articleToTag) {
        final Integer tagId = articleToTag.getTagId();
        final Integer articleId = articleToTag.getArticleId();
        QueryWrapper<ArticleToTag> wrapper = new QueryWrapper<>();
        LambdaQueryWrapper<ArticleToTag> lambda = wrapper.lambda();
        if (tagId!=null){
           lambda.eq(ArticleToTag::getTagId,tagId);
        }
        if (articleId!=null){
            lambda.eq(ArticleToTag::getArticleId,articleId);
        }
        return list(wrapper);
    }

    /**
     * 删除关系
     *
     * @param id 文章类型关系ID
     * @return boolean
     */
    @Override
    public boolean delete(Integer id) {
        return removeById(id);
    }

    /**
     * 存在关系
     *
     * @param articleToTag
     * @return
     */
    @Override
    public boolean isExist(ArticleToTag articleToTag) {
        return null!=getOne(articleToTag);
    }
}
